var ws;
var socketCreated = false;
var isLogout = false;
var userId;
var groupId;
$(document).ready(function() {
		open();
});
function open() {
	// 页面一加载就执行
	var div = document.getElementById('msgUl');// 页面加载就把滚动条拉到最下面
	div.scrollTop = div.scrollHeight;
	// 判断浏览器是否支持WebSocket
	if (window.WebSocket || window.MozWebSocket) {
		$("#msgUl").append(
				"<ul class='r-chat-thread'><li>当前浏览器支持webSocket</li></ul>");
		var div = document.getElementById('msgUl');
		div.scrollTop = div.scrollHeight;
	} else {
		$("#msgUl").append(
				"<ul class='r-chat-thread'><li>当前浏览器不支持webSocket</li></ul>");
		var div = document.getElementById('msgUl');
		div.scrollTop = div.scrollHeight;
	}
	// 绑定消息发送事件
	$("#sendMsgBtn").click(function() {
		var message = $("#msgContent").val().trim();
		if (message != "") {
			 //发送消息
			 var request = {
				 time : 1000,
				 type : MSG_TYPE_SENDMSG,//和枚举对应
				 obj : {
					 userId : userId,
					 groupId : groupId,
					 msg : message
				 }
			 }
			send(request);
		}
		$("#msgContent").val("");
		var div = document.getElementById('msgUl');
		div.scrollTop = div.scrollHeight;
	});
	// 绑定回车
	document.onkeydown = function(e) {
		if (!e) {
			e = window.event;
		}
		if ((e.keyCode || e.which) == 13) {
			var message = $("#msgContent").val().trim();
			if (message != "") {
				 //发送消息
				 var request = {
					 time : 1000,
					 type : MSG_TYPE_SENDMSG,//和枚举对应
					 obj : {
						 userId : userId,
						 groupId : groupId,
						 msg : message
					 }
				 }
				send(request);
			}
			$("#msgContent").val("");
			var div = document.getElementById('msgUl');
			div.scrollTop = div.scrollHeight;
		}
	}
	// 开始连接服务端
	// 0：正在连接 1：已连接 2：正在关闭连接 3：已关闭连接
	if (socketCreated && (ws.readyState == 0 || ws.readyState == 1)) {// 已经连接了
		send(userId, chatId, "谁离开了聊天室");
		socketCreated = false;
		isLogout = true;
		ws.onclose;
	} else {// 还没连接
		// 申请WebSocket对象 组装json
		if ("WebSocket" in window) {
			ws = new WebSocket("ws://" + doMain + prot);
		} else if ("MozWebSocket" in window) {
			ws = new MozWebSocket("ws://" + doMain + prot);
		}
		socketCreated = true;
		isLogout = false;
		// 注册事件
		ws.onopen = wsOnOpen;// 获得连接时
		ws.onmessage = wsOnMessage;// 接收消息时
		ws.onclose = wsOnClose;// 关闭连接时
		ws.onerror = wsOnError;// 处理发生错误时(消息发送错误、连接错误)
	}
}

function send(msg) {
	ws.send(JSON.stringify(msg));
}

function wsOnOpen() {
	//改造
	
	// 需要判断是创建房间还是加入房间
	userId = getCookie(COOKIE_USER_ID);
	groupId = requestParam("groupId");
	var msgType = requestParam("msgType");

	var request = {
		time : 1000,
		type : msgType,// 和枚举对应
		obj : {
			userId : userId,
			groupId : groupId
		}
	}
	send(request);
	$("#msgUl").show();
}

function wsOnMessage(event) {
	var json = event.data;
	console.log(json);
	var jsonObj = JSON.parse(json);
	console.log(jsonObj);
	
	//接收逻辑
	
	if(jsonObj.code != RESPONSE_OK ){
		alert("数据接收失败!");
		return;
	}
	if(jsonObj.data.obj.id == getCookie(COOKIE_USER_ID)){
		$("#msgUl").append("<ul class='r-chat-thread'><li>" + jsonObj.data.msg + "</li></ul>");
	}else{
		$("#msgUl").append("<ul class='l-chat-thread'><li>" + jsonObj.data.msg + "</li></ul>");
	}
	var div = document.getElementById('msgUl');// 接收消息也要控制滚动条那个jb玩意儿
	div.scrollTop = div.scrollHeight;
}

function wsOnClose() {
	ws.close();// 关闭连接
}

function wsOnError() {
	$("#msgUl").append("<li>连接错误，请检查...</li>");
	ws.error();
}
